<?php
include_once "xAP-functions/webincludes.php";
echo webpageheader();


$now=date("Y-m-d H:i:s");
//build array of times
if(isset($_GET["date"])){
	$t["start"]=date("Y-m-d H",intval($_GET["date"])).":00:00";
}else{
	$t["start"]=date("Y-m-d H").":00:00";
}
$t["end"]=date("Y-m-d H:i:00",strtotime($t["start"]." +2 hours"));
$t["st"]=strtotime($t["start"]);
$t["en"]=strtotime($t["end"]);

$sql="select xAP_TV_Prog.ChannelName,xAP_TV_Prog.ProgName,xAP_TV_Prog.ProgStart,xAP_TV_Prog.ProgEnd,
xAP_TV_Channel.ChannelAlias,xAP_TV_Prog.Category,xAP_TV_Prog.ProgDesc, 
xAP_TV_Prog.TriggerEvent
from xAP_TV_Prog 
INNER JOIN xAP_TV_Channel on xAP_TV_Channel.ChannelName=xAP_TV_Prog.ChannelName

where ProgEnd>='".$t["start"]."' and ProgStart<'".$t["end"]."'
order by xAP_TV_Channel.ChannelOrder
";
$results=dbquery($sql);

//display

foreach($results as $key=>$row){
	$grid[$row["ChannelName"]]["name"]=$row["ChannelAlias"];
	$s=strtotime($row["ProgStart"])/5/60;
	$e=strtotime($row["ProgEnd"])/5/60;
	for($i=$s;$i<$e;$i++){
		$grid[$row["ChannelName"]][$i]=$key;
		if(($i*5*60)>=$t["st"]){
			$results[$key]["Count"]++;
		}
	}
}


//fillgrid

$slots=(($t["en"]-$t["st"])/5/60);

echo "<table width=100%  border='1'><TR><TD boder=0 align=center>
<a href='grid.php?date=".(strtotime($t["start"])-(2*60*60))."'><img src='images/rev.gif'></a>&nbsp;
<a href='grid.php?date=".time()."'><img src='images/now.gif'></a>&nbsp;
<a href='grid.php?date=".(strtotime($t["start"])+(2*60*60))."'><img src='images/fwd.gif'></a>
</td>";

for($i=0;$i<$slots;$i++){
echo "<TD colspan=3 class='boldtext' width='".intval(100/$slots*3)."%'>".date("H:i",$t["st"]+($i*5*60))."</td>";
$i++;
$i++;
}
echo "</tr>";
echo "<TR height=3><TD></td>";
for($i=0;$i<$slots;$i++){
	if (time()>($t["st"]+($i*5*60)) && time()<($t["st"]+($i*5*60)+300)){
		$bg="bgcolor='#FF0000'";
	}else{
	$bg="";
	}
	echo "<TD width='".intval(100/$slots)."%' $bg></tD>";
}
echo "</tr>";


$lastentmp=date("Y-m-d H:i:s",$t["st"]); //hold a temp to allow for programs to span the edges of the grid
foreach($grid as $key=>$value){

	$lasten=date("Y-m-d H:i:s",$t["st"]);
	echo "<TR><TD><img src='images/channels/".$key.".jpg' height=50 width=66></td>";
	for($i=0;$i<$slots;$i++){
		$p=(($t["st"]/5/60)+($i)); // get programe number from 
		$cs=$results[$value[$p]]["Count"];
		$st=date("H:i",strtotime($results[$value[$p]]["ProgStart"]));
		$en=date("H:i",strtotime($results[$value[$p]]["ProgEnd"]));
		//round ending minutes
		$enmin=substr(date("i",strtotime($results[$value[$p]]["ProgEnd"])),-1);
		if ($enmin>5){
			$enmin=$enmin-5;
			
			$cs=$cs-1;
		}
		//deal with blanks
		if( strtotime($results[$value[$p]]["ProgStart"])>strtotime($lasten)){
			$x=(strtotime($st)-strtotime($lasten))/5/60;
			echo "<TD colspan=".$x."></td>";
		
		}
		if(isset($results[$value[$p]])){
			// hide program description for small slots
			$desc=$results[$value[$p]]["ProgDesc"];
			if($cs<6){$desc="";}
			
		echo "<TD valign=top class='normaltext' colspan=$cs bgcolor='".TVColourCategory($results[$value[$p]]["Category"])."'>";
		if($results[$value[$p]]["TriggerEvent"]!=""){
			echo "<span class='topright boldtext'><img src='images/star_blue.png'></span>";
		}		
		echo "<span class='boldtext'>".$results[$value[$p]]["ProgName"]."</span><BR>
		<span class='normalsmalltext'>".$st." to ".$en."<BR>".substr($desc,0,80)."</span>";

		echo "</td>";
		$lasten=$results[$value[$p]]["ProgEnd"];
		}
		elseif($$c>0)
		{
			echo "<TD colspan=$cs></td>";
		}
		$i=$i+$cs;
		
		
	}
	
	echo "</tr>";

}


echo "</table>";


echo webpagefooter();
function TVColourCategory($cat){
	$cl="#ddddff";
	switch (strtolower($cat)){
		case "comedy": $cl="#FFdddd";break;
		case "entertainment": $cl="#ddddFF";break;
		case "documentary": $cl="#ddffdd";break;
		case "music and arts": $cl="#aa99ff";break;
		case "sport": $cl="#ff99aa";break;
		case "film": $cl="#ffaa99";break;
		case "game show": $cl="#aaff99";break;
		case "children": $cl="#99ffaa";break;
		case "youth": $cl="#ee99aa";break;
		case "sitcom": $cl="#99ffff";break;
		case "education": $cl="#ff99ff";break;
		case "news and current affairs": $cl="#ffff99";break;
		case "drama": $cl="#ffddaa";break;
		case "talk show": $cl="#ffaadd";break;
		case "animation": $cl="#aaffdd";break;
		case "soap": $cl="#ddffaa";break;
		case "interests": $cl="#ddaaff";break;
		case "cookery": $cl="#aaaaaa";break;
		case "health": $cl="#cccccc";break;
		case "transport": $cl="#999999";break;
		case "travel": $cl="#fffacf";break;
		case "science": $cl="#ffaaff";break;
		case "no genre": $cl="#aaffff";break;
		case "current affairs": $cl="#9abd9a";break;
		case "consumer": $cl="#ffa9a9";break;
		case "business": $cl="#a5fffa";break;
		case "religion": $cl="#9999ff";break;
		case "environment": $cl="#99Ff99";break;
		case "tba": $cl="#999999";break;
		case "gardening": $cl="#ccffcc";break;
	}
	return $cl;
}
?>